import Cesium from '../../Ces/Cesium'

export default (arrayBuffer) => {
  const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  const sizeOfFloat32 = Float32Array.BYTES_PER_ELEMENT;
  const uint8Array = new Uint8Array(arrayBuffer);
  const view = new DataView(arrayBuffer);

  const header: any = {}

  let byteOffset = 0;

  byteOffset += 6;
  //@ts-ignore
  const t1 = String(Cesium.getStringFromTypedArray(uint8Array, byteOffset, 20)).replaceAll('\u0000', '');

  byteOffset += 20;
  //@ts-ignore
  const t2 = String(Cesium.getStringFromTypedArray(uint8Array, byteOffset, 20)).replaceAll('\u0000', '');

  byteOffset += 50;
  //@ts-ignore
  const t3 = String(Cesium.getStringFromTypedArray(uint8Array, byteOffset, 20)).replaceAll('\u0000', '');

  byteOffset += 30;
  header.featureType = t2;
  // console.info(t2);
  header.title = `${t1}-${t2}-${t3}`;
  header.layer = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.year = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.month = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.day = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.hour = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.timeZone = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.aging = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.stLon = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.edLon = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.resLon = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.numLon = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  header.stLat = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.edLat = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.resLat = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.numLat = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;

  header.stContour = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.edContour = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  header.intervalContour = parseFloat(view.getFloat32(byteOffset, true).toFixed(4));
  byteOffset += sizeOfFloat32;
  byteOffset += 100;

  const width = header.numLon;
  const height = header.numLat;

  const arrayBufferView = new Float32Array(width * height);
  for (let i = 0; i < arrayBufferView.length; i++, byteOffset += sizeOfFloat32) {
    arrayBufferView[i] = view.getFloat32(byteOffset, true);
  }

  const west = header.stLon;
  const north = header.stLat;
  const east = header.edLon;
  const south = header.edLat;

  return {
    source: {
      width,
      height,
      arrayBufferView,
      pixelFormat: Cesium.PixelFormat.R,
      pixelDatatype: Cesium.PixelDatatype.FLOAT,
    },
    west,
    south,
    east,
    north,
  }
}
